package com.jqsoft.launcher.activity;

import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.jqsoft.launcher.common.BphsConstants;
import com.jqsoft.launcher.R;
import com.jqsoft.launcher.util.MessageTools;

import org.apache.commons.lang.StringUtils;

/**ip地址设置界面
 * @author gujunqi
 * date：2014-3-14 
 */
public class SettingIPActivity extends JQActivity implements OnClickListener {

	private SharedPreferences loginlist = null;
	private Button btnSave;
	private EditText txtIP1,txtIP2,txtIP3,txtIP4;
	private EditText txtPort;
	private EditText txtServerName;
	@Override
	protected int getContentLayout() {
		return R.layout.setting_ip;
	}

	@Override
	protected void initGui() {
		btnSave=(Button) findViewById(R.id.btnSave);
		btnSave.setOnClickListener(this);
		txtIP1 = (EditText) findViewById(R.id.txtIP1);
		txtIP2 = (EditText) findViewById(R.id.txtIP2);
		txtIP3 = (EditText) findViewById(R.id.txtIP3);
		txtIP4 = (EditText) findViewById(R.id.txtIP4);
		txtPort = (EditText) findViewById(R.id.txtPort);
		txtServerName = (EditText) findViewById(R.id.txtServerName);
	}

	@Override
	protected void initAction() {
		setGoBack();

	}

	@Override
	protected void initData() {
		setTitle("设置IP地址");
		loginlist = getSharedPreferences("LOGIN_INFOS", 0);
		if(StringUtils.isBlank(loginlist.getString("ip", ""))){
			return;
		}
		String ips,port;
		if(BphsConstants.isProduct){
			ips= BphsConstants.HTTP_SERVER_IP;
			port= BphsConstants.HTTP_SERVER_PORT;
		}else{
			ips= BphsConstants.HTTP_TEST_SERVER_IP;
			port= BphsConstants.HTTP_TEST_SERVER_PORT;
		}
		String[] ipstr=ips.split("\\.");
		txtIP1.setText(ipstr[0]);
		txtIP2.setText(ipstr[1]);
		txtIP3.setText(ipstr[2]);
		txtIP4.setText(ipstr[3]);
		txtPort.setText(port);
		txtServerName.setText(BphsConstants.PROJECT_PATH);

	}
	public void exitDialog() {
		// 对话框
		Builder dialog = new Builder(this)
				.setMessage("保存IP成功！,系统将自动退出，请稍候重启！")
				.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								exitSystem();
							}
						});
		MessageTools.showDialog(dialog);
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnSave:
			String txtIP1_str = txtIP1.getText().toString().trim();
			String txtIP2_str = txtIP2.getText().toString().trim();
			String txtIP3_str = txtIP3.getText().toString().trim();
			String txtIP4_str = txtIP4.getText().toString().trim();
			String txtPort_str = txtPort.getText().toString().trim();
			String txtServerName_str = txtServerName.getText().toString().trim();
			
			if (StringUtils.isNotBlank(txtIP1_str)&&StringUtils.isNotBlank(txtIP2_str)&&StringUtils.isNotBlank(txtIP3_str)&&StringUtils.isNotBlank(txtIP4_str)){
			loginlist.edit().putString("ip", txtIP1_str+"."+txtIP2_str+"."+txtIP3_str+"."+txtIP4_str)
			.commit();}else{
				MessageTools.showDialogOk(ctx, "IP地址不能为空！");
				return;
			}
			if(StringUtils.isNotBlank(txtPort_str)){
				loginlist.edit().putString("port", txtPort_str)
				.commit();
			}else{
				MessageTools.showDialogOk(ctx, "端口号不能为空！");
				return;
			}
			if(StringUtils.isNotBlank(txtServerName_str)){
				loginlist.edit().putString("path", txtServerName_str)
				.commit();
			}
			BphsConstants.HTTP_SERVER_IP=loginlist.getString("ip", "");
			BphsConstants.HTTP_SERVER_PORT=loginlist.getString("port", "");
//			MessageTools.showDialogOkBack(act, "保存IP成功！") ;
			exitDialog();
			return;
		}
		
	}
}
